package slxy.jiangtao.dts.sys;

import com.lowagie.text.*;
import com.lowagie.text.rtf.RtfWriter2;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import slxy.jiangtao.dts.core.common.service.CommonService;
import slxy.jiangtao.dts.core.util.FileUtil;
import slxy.jiangtao.dts.core.util.JacobUtil;
import slxy.jiangtao.dts.entity.sys.News;

import java.io.*;
import java.util.List;

import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.html.simpleparser.StyleSheet;
import slxy.jiangtao.dts.entity.sys.SystemFiles;
import slxy.jiangtao.dts.service.sys.NewsService;

/**
 * Code must be sexy
 * Created by Jiang Tao on 2017/5/1.
 * 说明：
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
        {
                "classpath:spring.xml",
                "classpath:spring-hibernate.xml"
        }
)
public class Html2Word {
    @Autowired
    CommonService commonService;
    @Autowired
    NewsService newsService;

    @Test
    public void htmltoWord() throws Exception {
        OutputStream out = new FileOutputStream("e://b.doc");
        Document document = new Document(PageSize.A4);
        RtfWriter2.getInstance(document, out);
        document.open();
        Paragraph context = new Paragraph();
        String s = commonService.get(News.class, 12).getContent();
        System.out.println(s);
        // Image img = Image.getInstance("D:\\图片\\2.jpg");
        // img.setAbsolutePosition(0, 0);//
        // document.add(img);
        StyleSheet ss = new StyleSheet();
        List htmlList = HTMLWorker.parseToList(new StringReader(s), ss);
        for (int i = 0; i < htmlList.size(); i++) {
            com.lowagie.text.Element e = (com.lowagie.text.Element) htmlList
                    .get(i);
            context.add(e);
        }
        document.add(context);
        document.close();
        System.out.println("ok");

    }

    @Test
    public void htmlToWord2() throws Exception {
        OutputStream out = new FileOutputStream("e://a.doc");
        Document document = new Document(PageSize.A4);
        RtfWriter2.getInstance(document, out);
        document.open();
        Paragraph context = new Paragraph();
        String htmlContent =commonService.get(News.class, 18).getContent();;

        StyleSheet ss = new StyleSheet();
        List htmlList = HTMLWorker.parseToList(new StringReader(htmlContent), ss);
        for (int i = 0; i < htmlList.size(); i++){
            com.lowagie.text.Element e = (com.lowagie.text.Element) htmlList.get(i);
            context.add(e);
        }
        document.add(context);
        document.close();
        System.out.println("ok");
    }

    @Test
    public void htmlToWord3() throws Exception {
        News news = commonService.get(News.class, 18);
        newsService.saveWord(news);
    }

    @Test
    public void wordTohtml() throws IOException {
        SystemFiles systemFiles = commonService.get(SystemFiles.class, "402880e95bd3502f015bd35f77c60003");
        String path=systemFiles.getLocalPath() + File.separator + systemFiles.getNo() +"."+ "html";
        System.out.println(path);
        //JacobUtil.wordToHtml("F:\\个人\\毕设\\开题报告\\开题答辩—蒋桃\\蒋桃—任务书.doc","F:\\个人\\蒋桃—任务书.html");
        StringBuffer sb = FileUtil.readToBuffer(path,"gb2312");
        String fileName = new String(sb.toString());
        System.out.println(systemFiles.getPath());
        fileName=fileName.replaceAll("src=\"","src=\"/uploadPath/uploadify/"+systemFiles.getPath()+"/");
       System.out.println(fileName);


//        File file = new File(systemFiles.getLocalPath() + File.separator + systemFiles.getNo() +"."+ systemFiles.getSuffix());
//        String name = new String(FileUtil.readToBuffer(systemFiles.getLocalPath() + File.separator + systemFiles.getNo() +"."+ systemFiles.getSuffix(),"gb2312"));
//
//        System.out.println(name);
    }

}
